<template>
    <view class="content">
        <view class="search">
            <view class="s-left">省市区</view>
            <view class="s-inp" @click="regionShow = true">
                <input type="text" v-model="keyword" class="inp" disabled>
                <u-icon name="arrow-down-fill" color="#575757" size="24" class="u-m-l-10"></u-icon>
            </view>
            <view class="screen" @click="show = true">
                <image src="../../static/icon/screen.png" class="screen-icon"></image>
                <view>筛选</view>
            </view>
        </view>
        
        <view class="item" v-for="(item, index) in list" :key="index" @click="$gTo(`/pages/calf/calf-detail?id=${item.id}`)">
            <view>
                <image src="../../static/image/g_img_1.png" class="img-1" v-if="item.member_type_txt == '个人'"></image>
                <image src="../../static/image/g_img_2.png" class="img-1" v-else></image>
            </view>
            <view class="item-d">
                <view class="top2">
                    <view class="title">{{item.title}}</view>
                    <view class="sta">{{item.type_txt}}</view>
                    <!-- <view class="sta sta2">求购</view> -->
                </view>
                <view class="text">{{item.describe}}</view>
                <view class="img-box" v-if="item.image_uri_txt.length > 0">
                    <view class="u-rela" v-for="(el, ind) in item.image_uri_txt" :key="ind" v-if="ind < 3">
                        <image :src="el.attach_uri" class="img"></image>
                        <view class="img-num" v-if="item.image_uri_txt.length > 3 && ind == 2">+{{ item.image_uri_txt.length - 3 }}</view>
                    </view>
                </view>
                <view class="down">
                    <view v-if="item.telephone" @click.stop="goTel(item.telephone)">
                        <image src="../../static/icon/tel2.png" class="tel2"></image>
                        <view>电话</view>
                    </view>
                    <view @click.stop="doCollct(item)">
                        <!-- 0-未收藏 大于0已收藏 -->
                        <u-icon class="u-m-r-10" name="star" :color="item.collction_id > 0 ? '#f62a2a' : '#a3a3a3'" size="34"></u-icon>
                        <view>{{ item.collction_id > 0 ? '已收藏' : '收藏' }}</view>
                    </view>
                </view>
            </view>
        </view>
        
        <view style="height: 94rpx;padding-top: 30rpx;">
            <u-loadmore :status="status" v-if="status != 'loadmore'" />
        </view>
        
        <view class="footer" @click="add">发布信息</view>
        
        <u-popup v-model="show" mode="right" width="600rpx" height="100vh">
            <view class="popup">
                <view class="pop" style="height: 93vh;">
                    <scroll-view scroll-y style="height: 93vh;">
                        <view>
                            <view class="p-tit" v-if="data && data[0] && data[0].name">{{ data[0].name }}</view>
                            <view class="p-box">
                                <view class="p-item" v-for="(item, index) in typeList" :key="index"
                                    :class="{pActive : item.isSelect}" @click="chooseType(item)">{{item.name}}</view>
                            </view>
                        </view>
                        <view>
                            <view class="p-tit" v-if="data && data[1] && data[1].name">{{ data[1].name }}</view>
                            <view class="p-box">
                                <view class="p-item" v-for="(item, index) in calfList" :key="index"
                                    :class="{pActive : item.isSelect}" @click="chooseCalf(item)">{{item.name}}</view>
                            </view>
                        </view>
                    </scroll-view>
                </view>
                <view class="p-btns">
                    <view class="p-btn" @click="reset">重置</view>
                    <view class="p-line"></view>
                    <view class="p-btn" @click="confirmScreen">确定</view>
                </view>
            </view>
        </u-popup>
        
        <!-- 省市区 -->
        <u-select v-model="regionShow" mode="mutil-column-auto" :list="region" value-name="region_id" label-name="name" child-name="sub" 
            @confirm="confirm"></u-select>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                regionShow: false,
                region: [],
                keyword: '请选择',
                province: '',
                city: '',
                district: '',
                show: false,
                status: 'loadmore', //是否显示没有更多了
                page: 1,
                list: [],
                data: [],
                typeList: [],//信息类型
                calfList: [],//牛只类型
                screenId: [],//筛选关键词
                type: '',
            };
        },
        onLoad() {
			
			this.keyword = '内蒙古自治区/通辽市'
			this.province = 150000
			this.city = 150500
            this.getRegion()
            this.getOption()
        },
        onShow() {
            this.list = []
            this.page = 1
            this.status = 'loadmore'
            this.getList()
        },
        onReachBottom() {
        	if(this.page == 1){return}
        	this.getList()
        },
        methods: {
			add() {
				if (!uni.getStorageSync('userToken')) {
				    this.$confirm('请先登录并且绑定手机号，是否去登录？',()=>{
				        uni.navigateTo({
				            url: "/pages/login/login"
				        })
				    })
				} else {
					this.$gTo(`/pages/calf/create`)
				}
			},
			
			goTel(t) {
				this.$call(t)
			},
			
            // 收藏
            doCollct(item) {
				if (!uni.getStorageSync('userToken')) {
				    this.$confirm('请先登录并且绑定手机号，是否去登录？',()=>{
				        uni.navigateTo({
				            url: "/pages/login/login"
				        })
				    })
				} else {
					this.$ajax('addCollection', {
						userToken: this.$getSync('userToken'),
					    data_id: item.id,
					    data_type: 'calf',//product商品 calf牛源
					}).then(ret => {
						if (ret.status == 0) {
							item.collction_id = item.collction_id > 0 ? 0 : 1
						} else {
							this.$toast(ret.message);
						}
					});
				}
            },
            
            // 重置
            reset() {
                if (this.typeList.length > 0) {
                    this.typeList.forEach(ii => {
                        ii.isSelect = false;
                    })
                }
                if (this.calfList.length > 0) {
                    this.calfList.forEach(ii => {
                        ii.isSelect = false;
                    })
                }
                this.type = ''
                this.screenId = []
                this.list = []
                this.page = 1
                this.status = 'loadmore'
                this.getList()
            },
            
            // 确定
            confirmScreen() {
                if (this.typeList.length > 0) {
                    this.typeList.forEach(ii => {
                        if (ii.isSelect) {
                            this.type = ii.value
                        }
                    })
                }
                if (this.calfList.length > 0) {
                    this.calfList.forEach(ii => {
                        if (ii.isSelect) {
                            this.screenId.push(ii.value)
                        }
                    })
                }
                
                this.list = []
                this.page = 1
                this.status = 'loadmore'
                this.getList()
                this.show = false
            },
            
            // 选择信息类型
            chooseType(item) {
                const nowStatu = item.isSelect
                this.typeList.forEach(ii => {
                    ii.isSelect = false;
                })
                item.isSelect =! nowStatu
            },
            
            // 选择牛只
            chooseCalf(item) {
                item.isSelect = !item.isSelect
            },
            
            // 获取列表
            getList() {
            	if (this.status == 'nomore') return;
            	this.status = 'loading';
            	this.$ajax('LnformationList', {
            		userToken: this.$getSync('userToken'),
            		page: this.page,
            		limit: 10,
                    category: 1,//1-牛源信息 2-人力场地信息
                    type: this.type,//category=1的时候type 1-出售 2-是求购，category=2的时候 type是 1-招聘 2-求职 3-出租/出售 4-求租/求购 5-其他
                    gender: this.screenId.join(',') || '',//公牛/母牛 男/女
                    province: this.province,
                    city: this.city,
                    district: this.district,
            	}).then(ret => {
            		if (ret.status == 0) {
            			if (ret.data.list.length > 0) {
            				this.list = this.list.concat(ret.data.list);
            				this.page++;
            				this.status = 'loadmore'
            			} else {
            				this.status = 'nomore'
            			}
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
            
            // 选择省市区
            confirm(e) {
                console.log(e);
                this.keyword = e[0].label + e[1].label + e[2].label
                this.province = e[0].value
                this.city = e[1].value
                this.district = e[2].value
                
                this.list = []
                this.page = 1
                this.status = 'loadmore'
                this.getList()
            },
            
            // 获取省市区
            getRegion(){
            	this.$ajax('region', {
            		
            	}).then(ret => {
            		if (ret.status == 0) {
            			this.region = ret.data
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
            
            // 获取筛选项
            getOption(){
            	this.$ajax('searchLnformationOption', {
            		userToken: this.$getSync('userToken'),
            	}).then(ret => {
            		if (ret.status == 0) {
            			this.data = ret.data
                        if (ret.data && ret.data[0] && ret.data[0].list) {
                            this.typeList = ret.data[0].list//信息类型
                            this.typeList.forEach(cur => {
                                this.$set(cur, 'isSelect', false)
                            })
                        }
                        
                        if (ret.data && ret.data[1] && ret.data[1].list) {
                            this.calfList = ret.data[1].list//牛只类型
                            this.calfList.forEach(cur => {
                                this.$set(cur, 'isSelect', false)
                            })
                        }
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
        }
    };
</script>

<style lang="scss">
    
    
    page{
        background-color: #f5f5f5;
    }
    .content{
        padding: 20rpx 20rpx 100px;
    }
    .search{
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 26rpx;
        margin-bottom: 20rpx;
    }
    .s-left{
        color: #737373;
        padding-right: 20rpx;
    }
    .s-inp{
        flex: 1;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15rpx;
        background-color: #fff;
        border-radius: 15rpx;
    }
    .inp{
        flex: 1;
    }
    .screen{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 25rpx;
    }
    .screen-icon{
        width: 26rpx;
        height: 21rpx;
        margin-right: 5rpx;
    }
    
    
    .item{
        background-color: #fff;
        border-radius: 15rpx;
        margin-bottom: 20rpx;
    }
    .img-1{
        width: 90rpx;
        height: 42rpx;
    }
    .item-d{
        padding: 10rpx 30rpx 30rpx;
    }
    .top2{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .title{
        font-size: 34rpx;
        font-weight: bold;
        word-break: break-all;
        white-space: pre-line;
        padding-right: 15rpx;
    }
    .sta{
        padding: 4rpx 15rpx;
        border-radius: 6rpx;
        font-size: 23rpx;
        color: #ff5f2b;
        background-color: #fff1ec;
    }
    .sta2{
        color: #0075ff;
        background-color: #dfeeff;
    }
    .text{
        font-size: 28rpx;
        word-break: break-all;
        white-space: pre-line;
        padding-top: 15rpx;
    }
    .img-box{
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-top: 10rpx;
    }
    .img{
        width: 200rpx;
        height: 200rpx;
        border: 2rpx solid #dedede;
        border-radius: 10rpx;
        margin-right: 20rpx;
        box-sizing: border-box;
    }
    .img:nth-child(3n) {
        margin-right: 0;
    }
    .img-num{
        font-size: 26rpx;
        color: #fff;
        text-align: center;
        line-height: 200rpx;
        width: 200rpx;
        height: 200rpx;
        border-radius: 10rpx;
        background: rgba(0, 0, 0, .5);
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
    }
    .down{
        font-size: 26rpx;
        color: #595b62;
        padding-top: 20rpx;
        display: flex;
        justify-content: flex-end;
        align-items: center;
    }
    .down>view{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 50rpx;
    }
    .tel2{
        width: 27rpx;
        height: 27rpx;
        margin-right: 10rpx;
    }
    
    .footer{
        width: 100%;
        height: 90rpx;
        background-color: #00734c;
        font-size: 36rpx;
        color: #fff;
        text-align: center;
        line-height: 90rpx;
        position: fixed;
        left: 0;
        bottom: 0;
        z-index: 9;
    }
    
    
    .popup{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    .pop{
        padding: 0 30rpx;
    }
    .p-tit{
        font-size: 32rpx;
        font-weight: bold;
        padding: 20rpx 0;
    }
    .p-box{
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        align-items: center;
    }
    .p-item{
        font-size: 26rpx;
        padding: 20rpx 50rpx;
        background-color: #f5f5f5;
        border-radius: 15rpx;
        margin-right: 15rpx;
        margin-bottom: 15rpx;
    }
    .pActive{
        color: #083d2b;
        border: 2rpx solid #083d2b;
        background-color: #d6ebe4;
        box-sizing: border-box;
    }
    .p-btns{
        height: 7vh;
        background-color: #ff8a00;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .p-btn{
        width: calc(588rpx / 2);
        font-size: 30rpx;
        color: #fff;
        text-align: center;
    }
    .p-line{
        width: 2rpx;
        height: 37rpx;
        background-color: #fff;
    }

</style>
